package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.SetOnshelfBean;
import com.shop2cn.iapi.agentproducts.service.SetOnshelfCall;
import com.shop2cn.iapi.agentproducts.service.SetOnshelfCallV2;
import com.ymatou.iapi.prodstock.parameter.AgentProductPo;
import com.ymatou.mq.admin.parameter.MessageListBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.MqQueryCallService;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;

/**
*上架
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_SetOnshelf{
	private static SetOnshelfBean setonshelfBean;
	private static SetOnshelfCall setonshelfCall;
	private static YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
	//渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	//渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";
	//渠道商->代理商2
	private static int agentUserId2 = AgentUsers.agentUserId2;
	private static String agentUserName2 = "NickautoTest";

	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	//vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("上架");
	}

	@Before
	public void caseUp() {
		setonshelfBean=new SetOnshelfBean();
		setonshelfCall=new SetOnshelfCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(setonshelfCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verifyMongoCurMq(String pid,int count) throws ClientProtocolException, IOException, InterruptedException {
		Thread.sleep(5000);
		MessageListBean MessageListBean2 = MqQueryCallService.initAgentMongoCrud(pid);
		List<Map> joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
		int redo=0;
		while(joa2.size()<1&&redo<3) {
			Thread.sleep(5000);
			joa2 = MqQueryCallService.GetMqMessage(MessageListBean2);
			redo++;
		}
		Logger.debug(joa2);
		String actionType = "AgentProductOnShelfSingle";
		String appId = "agentproducts.iapi.ymatou.com";
		Logger.verifyEquals(count,joa2.size(),"验证发送两条消息到总线");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_01() {
		Logger.start(true,"下架商品上架，上架成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,rootUserId);
			setonshelfBean.setUserId(rootUserId);
			setonshelfBean.setProductId(productId);
			setonshelfCall.setData(setonshelfBean);
			setonshelfCall.callService();
			Logger.verifyEquals("200",setonshelfCall.getString("code"),"验证code");
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			Logger.verifyEquals(1,agentProductPo.getStatus(),"验证status");
			verifyMongoCurMq(productId,3);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_02() {
		Logger.start(true,"非渠道商上架商品,无权限，上架失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(agentUserId1,agentUserName1,productId,rootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,rootUserId);
			setonshelfBean.setUserId(agentUserId1);
			setonshelfBean.setProductId(agentProductId);
			setonshelfCall.setData(setonshelfBean);
			setonshelfCall.callService();
			Logger.verifyEquals("201",setonshelfCall.getString("code"),"验证code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_03() {
		Logger.start(true,"无效商品上架，上架失败");
		try {
			setonshelfBean.setUserId(rootUserId);
			setonshelfBean.setProductId("111");
			setonshelfCall.setData(setonshelfBean);
			setonshelfCall.callService();
			Logger.verifyEquals("201",setonshelfCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_04() {
		Logger.start(true,"渠道商保存商品上架，上架成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			addAgentProductBean.setSaleType(2);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			Logger.verifyEquals(2,agentProductPo.getStatus(),"验证商品是下级状态");
			setonshelfBean.setUserId(rootUserId);
			setonshelfBean.setProductId(productId);
			setonshelfCall.setData(setonshelfBean);
			setonshelfCall.callService();
			Logger.verifyEquals("200",setonshelfCall.getString("code"),"验证code");
			Logger.verifyEquals(1,ymtProductsIWapper.selectAgentProductByProductId(productId).getStatus(),"验证商品是上架状态");
			verifyMongoCurMq(productId,2);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_05() {
		Logger.start(true,"渠道商保存商品上架，userId为空，失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,true);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			setonshelfBean.setUserId(0);
			setonshelfBean.setProductId(productId);
			setonshelfCall.setData(setonshelfBean);
			setonshelfCall.callService();
			Logger.verifyEquals("400",setonshelfCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_06() {
		Logger.start(true,"渠道商客服上架商品，templateId1，失败");
		try {
			int csid = 500023289;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,vipRootUserId);
			setonshelfBean.setUserId(csid);
			setonshelfBean.setProductId(productId);
			SetOnshelfCallV2 setOnshelfCallV2 = new SetOnshelfCallV2();
			setOnshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767915F5F242E8D33853E112C14B8904A6AE559730333EF9D3573D244B59588F70E2BE7FDEE32326B219044BC1E17CF19372");

			setOnshelfCallV2.setData(setonshelfBean);
			setOnshelfCallV2.callService();
			Logger.verifyEquals("201",setOnshelfCallV2.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_07() {
		Logger.start(true,"渠道商客服上架商品，templateId2，失败");
		try {
			int csid = 500023288;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,vipRootUserId);
			setonshelfBean.setUserId(csid);
			setonshelfBean.setProductId(productId);
			SetOnshelfCallV2 setOnshelfCallV2 = new SetOnshelfCallV2();
			setOnshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D767954A3F01B09D21A20479176177E62025B342BD096EA5F576089601F8445F4D7D4D699DCA8A26A1614D518EDA2019F0532");

			setOnshelfCallV2.setData(setonshelfBean);
			setOnshelfCallV2.callService();
			Logger.verifyEquals("201",setOnshelfCallV2.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_08() {
		Logger.start(true,"渠道商客服上架商品，templateId3，成功");
		try {
			int csid = 500023287;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,vipRootUserId);
			setonshelfBean.setUserId(csid);
			setonshelfBean.setProductId(productId);
			SetOnshelfCallV2 setOnshelfCallV2 = new SetOnshelfCallV2();
			setOnshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D7679B8B5287DA8F6C358B032C5D4BF69995D777F8750D5FABCF6A03EBC4B67F0B3347F243B92D24DC8EAB34A7E1D0816E1CA");

			setOnshelfCallV2.setData(setonshelfBean);
			setOnshelfCallV2.callService();
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			Logger.verifyEquals(1,agentProductPo.getStatus(),"验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetOnshelf_09() {
		Logger.start(true,"渠道商客服上架商品，templateId4，成功");
		try {
			int csid = 500023286;
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddAgentProductCallService.SetOffshelf(productId,vipRootUserId);
			setonshelfBean.setUserId(csid);
			setonshelfBean.setProductId(productId);
			SetOnshelfCallV2 setOnshelfCallV2 = new SetOnshelfCallV2();
			setOnshelfCallV2.addHeader("ymt-pars","accesstoken=C73D22F7D46D76799A6A0A7DFE5EABBBFADB332C40F560517F6D23115912D2A9D66752A159D46C5EF288EA2ACD7155EE0F7923987B3F24F5");

			setOnshelfCallV2.setData(setonshelfBean);
			setOnshelfCallV2.callService();
			AgentProductPo agentProductPo = ymtProductsIWapper.selectAgentProductByProductId(productId);
			Logger.verifyEquals(1,agentProductPo.getStatus(),"验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}